package com.tts.superstar;


import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.tts.superstar.View.BaseActivity;
import com.tts.superstar.fragment.MessageFragment;
import com.tts.superstar.fragment.MyFragment;
import com.tts.superstar.fragment.NoteFragment;
import com.tts.superstar.fragment.ShouyeFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    private ImageButton mBtnMain1;
    private ImageButton mBtnMain2;
    private ImageButton mBtnMain3;
    private ImageButton mBtnMain4;
    private List<ImageButton> list;
    private List<Integer> imgResource_Black;
    private List<Integer> imgResource_Blue;
    private List<Fragment> fragmentList;
    private List<String> lists;
    private List<TextView> textViewList;
    /**
     * 首页
     */
    private TextView mTextTitle;
    private TextView mText1,mText2,mText3,mText4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        setFragment(new ShouyeFragment());
    }

    private void setFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.layout_main_fragment, fragment);
        transaction.commit();

    }

    private void initView() {
        mText1 = findViewById(R.id.text_main_1);
        mText2 = findViewById(R.id.text_main_2);
        mText3 = findViewById(R.id.text_main_3);
        mText4 = findViewById(R.id.text_main_4);
        mBtnMain1 = (ImageButton) findViewById(R.id.btn_main_1);
        mBtnMain2 = (ImageButton) findViewById(R.id.btn_main_2);
        mBtnMain3 = (ImageButton) findViewById(R.id.btn_main_3);
        mBtnMain4 = (ImageButton) findViewById(R.id.btn_main_4);
        list = new ArrayList<>();
        lists = new ArrayList<>();
        textViewList = new ArrayList<>();
        lists.add("首页");
        lists.add("消息");
        lists.add("我的");
        lists.add("我");
        textViewList.add(mText1);
        textViewList.add(mText2);
        textViewList.add(mText3);
        textViewList.add(mText4);
        imgResource_Black = new ArrayList<>();
        imgResource_Blue = new ArrayList<>();
        fragmentList = new ArrayList<>();
        fragmentList.add(new ShouyeFragment());
        fragmentList.add(new MessageFragment());
        fragmentList.add(new NoteFragment());
        fragmentList.add(new MyFragment());
        list.add(mBtnMain1);
        list.add(mBtnMain2);
        list.add(mBtnMain3);
        list.add(mBtnMain4);
        imgResource_Black.add(R.mipmap.sy_black);
        imgResource_Black.add(R.mipmap.xx_black);
        imgResource_Black.add(R.mipmap.bj_black);
        imgResource_Black.add(R.mipmap.me_black);
        imgResource_Blue.add(R.mipmap.sy_blue);
        imgResource_Blue.add(R.mipmap.xx_blue);
        imgResource_Blue.add(R.mipmap.bj_blue);
        imgResource_Blue.add(R.mipmap.me_blue);
        setClickLis();
        mTextTitle = (TextView) findViewById(R.id.text_title);
    }

    private void setClickLis() {
        for (int i = 0; i < list.size(); i++) {
            final int a = i;
            list.get(a).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    textViewList.get(a).setTextColor(Color.parseColor("#1295DA"));
                    mTextTitle.setText(lists.get(a));
                    setFragment(fragmentList.get(a));
                    for (int o = 0; o < list.size(); o++) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                            list.get(o).setBackground(getResources().getDrawable(imgResource_Black.get(o)));
                        }
                        if(a != o){
                            textViewList.get(o).setTextColor(Color.BLACK);
                        }
                    }
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        view.setBackground(getResources().getDrawable(imgResource_Blue.get(a)));
                    }
                }
            });
        }


    }
}
